package com.xfatm.util.date;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    /**
     * 获取指定时间周的Date数据，默认每周第一天为周一
     * @param date 指定时间
     * @return
     */
    public static List<Date> getThisWeekDate(Date date){
        List<Date> dates=new ArrayList<>(7);
        Calendar cal=Calendar.getInstance();
        cal.setTime(getWeekFirstDay(date,Calendar.MONDAY));
        dates.add(cal.getTime());
        for(int i=0;i<6;i++){
            cal.add(Calendar.DATE,1);
            dates.add(cal.getTime());
        }
        return dates;
    }

    /**
     * 获取指定时间周的第一天
     * @param date 指定之间
     * @param firstDay 第一天是星期几，如 Calendar.MONDAY
     * @return 每周第一天
     */
    public static Date getWeekFirstDay(Date date,int firstDay){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, firstDay);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date date2 = cal.getTime();
        return date2;
    }

    /**
     * 获取指定时间周的最后
     * @param date 指定之间
     * @param firstDay 第一天是星期几，如 Calendar.MONDAY
     * @return 每周第一天
     */
    public static Date getWeekLastDay(Date date,int firstDay){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getWeekFirstDay(date,firstDay));
        cal.add(Calendar.DATE,6);
        return cal.getTime();
    }
}
